package com.kidedu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import com.kidedu.dao.mapper.KArtMapper;
import com.kidedu.dao.mapper.KGradeMapper;
import com.kidedu.dao.mapper.KStudentMapper;
import com.kidedu.entity.KArt;
import com.kidedu.entity.KArtExample;
import com.kidedu.entity.KGrade;
import com.kidedu.entity.KGradeExample;
import com.kidedu.entity.KStudent;
import com.kidedu.entity.KStudentExample;
import com.kidedu.entity.dto.KArtDTO;
import com.kidedu.service.IKArtService;
import com.kidedu.util.BaseService;
import com.kidedu.util.Pagination;

public class KArtService extends BaseService implements IKArtService{
	
	@Autowired
	private KArtMapper kArtMapper;
	
	@Autowired
	private KGradeMapper kGradeMapper;
	
	@Autowired
	private KStudentMapper kStudentMapper;
	
	private int pageSize = 9;

	/**
	 * 根据时间筛选出优秀作品，提供分页功能。
	 */
	public Pagination<KArtDTO> selectKArtListByDate(Date startTime, Date endTime, String keyword, int pageNo) {
		//班级编号列表
		List<String> kGradeIdList = new ArrayList<String>();
		//学生编号列表
		List<String> kStudentIdList = new ArrayList<String>();
		
		KArtExample kArtExample = new KArtExample();
		com.kidedu.entity.KArtExample.Criteria c1 = kArtExample.createCriteria();
		com.kidedu.entity.KArtExample.Criteria c2 = kArtExample.createCriteria();
		c1.andAcreattimeBetween(startTime, endTime);
		//判断是否输入关键字
		if(keyword != null && !keyword.trim().equals("")){
			keyword = "%" + keyword + "%";
			//搜索可能的班级
			KGradeExample kGradeExample = new KGradeExample();
			kGradeExample.createCriteria().andGnameLike(keyword);
			List<KGrade> kGradeList = kGradeMapper.selectByExample(kGradeExample);
			if(kGradeList.size() > 0){
				for(int i = 0; i < kGradeList.size(); i++){
					kGradeIdList.add(kGradeList.get(i).getGid());
				}
				c1.andAgradeidIn(kGradeIdList);
			}
			//搜索可能的孩子
			KStudentExample kStudentExample = new KStudentExample();
			kStudentExample.createCriteria().andSusernameLike(keyword);
			List<KStudent> kStudentList = kStudentMapper.selectByExample(kStudentExample);
			if(kStudentList.size() > 0){
				for(int i = 0; i < kStudentList.size(); i++){
					kStudentIdList.add(kStudentList.get(i).getSid());
				}
				if(kGradeList.size() > 0){
					c2.andAcreattimeBetween(startTime, endTime);
					c2.andAstudentidIn(kStudentIdList);
					kArtExample.or(c2);
				}else{
					c1.andAstudentidIn(kStudentIdList);
				}
			}
			if(kStudentList.size() == 0 && kGradeList.size() == 0){
				return new Pagination<KArtDTO>(pageNo, pageSize, 0, new ArrayList<KArtDTO>());
			}
		}
		int totalCount = kArtMapper.countByExample(kArtExample);
		List<KArt> kArtList = kArtMapper.selectByExample(kArtExample, new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
		
		List<KArtDTO> kArtDTOList = new ArrayList<KArtDTO>();
		for(KArt kart : kArtList){
			KStudent kStudent = kStudentMapper.selectByPrimaryKey(kart.getAstudentid());
			KArtDTO kArtDTO = new KArtDTO(kart, kStudent);
			kArtDTOList.add(kArtDTO);
		}
		
		return new Pagination<KArtDTO>(pageNo, pageSize, totalCount, kArtDTOList);
	}
	
	
	public KArt ArtDetail(Byte A_id) {
		KArtExample kArtExample = new KArtExample();
		kArtExample.createCriteria().andAidEqualTo(A_id);
		List<KArt> kart=kArtMapper.selectByExample(kArtExample);
		if(kart==null||kart.size()<=0)	
		return null;
		return kart.get(0);
	}

	/*修改优秀作品*/
	public KArt updateKArt(Byte A_id,String comment,Byte A_type) {
		KArt kart = new KArt();
		kart.setAid(A_id);
		kart.setAcomment(comment);
		kart.setAtype(A_type);
		kArtMapper.updateByPrimaryKeySelective(kart);
		return kart;
	}
	
	/*删除作品*/
	public KArt deleteKArt(Byte A_id) {
		kArtMapper.deleteByPrimaryKey(A_id);
		return null;
	}
	
	/*上传作品*/
	public KArt addKArt(String work,String filename,String student,String grade,String comment,Byte A_id,String t_id){
		KArt kArt = new KArt();
		
		kArt.setAcontent(work+":"+filename);
		kArt.setAcomment(comment);
		kArt.setAteacherid(t_id);
		kArt.setAtype(A_id);
		
		KGradeExample kGradeExample = new KGradeExample();
		kGradeExample.createCriteria().andGnameEqualTo(grade);
		kArt.setAgradeid(kGradeMapper.selectByExample(kGradeExample).get(0).getGclassid());
		
		KStudentExample kStudentExample = new KStudentExample();
		kStudentExample.createCriteria().andSusernameEqualTo(student);
		kArt.setAstudentid(kStudentMapper.selectByExample(kStudentExample).get(0).getSid());
		
		kArtMapper.insertSelective(kArt);
		
		return kArt;
	}
	
	
	/*班主任查看作品*/
	public Pagination<KArtDTO> selectKArtListByTea(Date startTime, Date endTime, String keyword, int pageNo, String t_id) {
		//班级编号列表
		List<String> kGradeIdList = new ArrayList<String>();
		//学生编号列表
		List<String> kStudentIdList = new ArrayList<String>();
		
		KArtExample kArtExample = new KArtExample();
		com.kidedu.entity.KArtExample.Criteria c1 = kArtExample.createCriteria();
		com.kidedu.entity.KArtExample.Criteria c2 = kArtExample.createCriteria();
		c1.andAcreattimeBetween(startTime, endTime);
		c1.andAteacheridEqualTo(t_id);
		kArtExample.setOrderByClause("acreattime desc");
		//判断是否输入关键字
		if(keyword != null && !keyword.trim().equals("")){
			System.out.println("has keyword");
			keyword = "%" + keyword + "%";
			//搜索可能的班级
			KGradeExample kGradeExample = new KGradeExample();
			kGradeExample.createCriteria().andGnameLike(keyword);
			List<KGrade> kGradeList = kGradeMapper.selectByExample(kGradeExample);
			if(kGradeList.size() > 0){
				for(int i = 0; i < kGradeList.size(); i++){
					kGradeIdList.add(kGradeList.get(i).getGid());
				}
				c1.andAgradeidIn(kGradeIdList);
			}
			//搜索可能的孩子
			KStudentExample kStudentExample = new KStudentExample();
			kStudentExample.createCriteria().andSusernameLike(keyword);
			List<KStudent> kStudentList = kStudentMapper.selectByExample(kStudentExample);
			if(kStudentList.size() > 0){
				for(int i = 0; i < kStudentList.size(); i++){
					kStudentIdList.add(kStudentList.get(i).getSid());
				}
				if(kGradeList.size() > 0){
					c2.andAcreattimeBetween(startTime, endTime);
					c2.andAstudentidIn(kStudentIdList);
					kArtExample.or(c2);
				}else{
					c1.andAstudentidIn(kStudentIdList);
				}
			}
			if(kStudentList.size() == 0 && kGradeList.size() == 0){
				return new Pagination<KArtDTO>(pageNo, pageSize, 0, new ArrayList<KArtDTO>());
			}
		}
		int totalCount = kArtMapper.countByExample(kArtExample);
		List<KArt> kArtList = kArtMapper.selectByExample(kArtExample, new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
		
		List<KArtDTO> kArtDTOList = new ArrayList<KArtDTO>();
		for(KArt kart : kArtList){
				KStudent kStudent = kStudentMapper.selectByPrimaryKey(kart.getAstudentid());
				KArtDTO kArtDTO = new KArtDTO(kart, kStudent);
				kArtDTOList.add(kArtDTO);				
		}
		return new Pagination<KArtDTO>(pageNo, pageSize, totalCount, kArtDTOList);
	}
}